//
//  SEGCardPageControl.m
//  SEGActivity
//
//  Created by 山刘 on 2020/8/5.
//

#import "SEGCardPageControl.h"
@interface SEGCardPageControl()
@property(nonatomic,strong) UIStackView *stackView;
@end
@implementation SEGCardPageControl

+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        [self addSubview:self.stackView];
        
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    }
    return self;
}

#pragma mark - Getter

-(UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [[UIStackView alloc] init];
        _stackView.alignment = UIStackViewAlignmentCenter;
        _stackView.distribution = UIStackViewDistributionFillEqually;
        _stackView.spacing = 5;
    }
    return _stackView;
}

#pragma mark - Setter

-(void)setTotalPages:(NSInteger)totalPages {
    _totalPages = totalPages;
    
    for (UIView *object in self.stackView.subviews) {
        [object removeFromSuperview];
    }
    
    for (NSInteger i = 0; i < totalPages; i++) {
        UIView *pageView = [[UIView alloc] init];
        pageView.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.5];
        pageView.layer.cornerRadius = 3;
        pageView.layer.masksToBounds = YES;
        pageView.clipsToBounds = true;
        pageView.tag = i;
        
        [self.stackView addArrangedSubview:pageView];
        [pageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(@(CGSizeMake(6, 6))).priorityHigh();
        }];
    }
}

-(void)setCurrentIndex:(NSInteger)currentIndex {
    _currentIndex = currentIndex;
    
    [self.stackView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIColor *colA = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]; //完全不透明白色
        UIColor *colB = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.5];//半透明白色
        obj.backgroundColor = obj.tag == currentIndex ? colA : colB;
    }];
}

@end
